import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

//
//  @ Project : Project1
//  @ File Name : GameView.java
//  @ Date : 1/21/2013
//  @ Author : Graham Tapscott
//
//



/***
 * GameView - this class is used to hold the user interface components and contains methods
 * to allow the UI components to be updated and/or read
 * @author gtapscott
 *
 */
public class GameView extends JFrame {

	// Constants
	private static final String WINDOW_TITLE = "CMSC 335 - Project 1";	
	private static final String[] SEARCH_INDEX = {"Index","Name","Type"};
	private static final long serialVersionUID = 7159268238977959390L;
	
    // Instance variables
    private JTextField searchInputTf = new JTextField(20);
    private JTextArea resultsTa     = new JTextArea(40,80);
    private JButton    searchBtn = new JButton("Search");
    private JButton    selectFileBtn    = new JButton("Select File");    
	private JFileChooser gameFileFc = new JFileChooser(".");
	private JComboBox<String> searchTypeCb = new JComboBox<String>(SEARCH_INDEX);
	private GameModel _model;
	
	public GameView(GameModel model) {
		// base logic
		_model = model;
		
        // Initialize components from the model
        searchInputTf.setText(_model.getSearchText());
        resultsTa.setText(_model.getResultsText());
		
		// Layout UI components

        // content panel # 1
		JPanel content = new JPanel();	
        content.setLayout(new FlowLayout());
        content.add(selectFileBtn);
        
        content.add(new JLabel("Search For:"));        
        content.add(searchTypeCb);
        // set default selection for the combobox
        searchTypeCb.setSelectedIndex(_model.getSelectedIndex());
        
        content.add(new JLabel("String"));
        content.add(searchInputTf);
        content.add(searchBtn);
        
        // content panel # 2
        JPanel content2 = new JPanel();
        content2.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Results"),
                BorderFactory.createEmptyBorder(2,2,2,2)));
        content2.setLayout(new BorderLayout());
        content2.add(resultsTa);		
        
        // add content panels
        this.add(content,BorderLayout.NORTH);
        this.add(content2,BorderLayout.SOUTH);
        this.pack();

        // set the window title
        this.setTitle(WINDOW_TITLE);
 
        // Handle the closing event
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
	
	}
	
	public void reset() {	
	}
	/***
	 * getUserInput - userInput getter
	 * @return
	 */
	public String getUserInput() {
		return searchInputTf.getText();
	}
	
	/***
	 * getSearchType - searchTypecb getter
	 * @return
	 */
	public int getSearchType() {
		return searchTypeCb.getSelectedIndex();
	}	
	
	/***
	 * addFileChooserListener - adds actionlistener for file chooser
	 * @param fcal
	 */
    public void addFileChooserListener(ActionListener fcal) {
    	selectFileBtn.addActionListener(fcal);
    }
    
    /***
     * addSearchBtnListener - adds actionlistener for search button
     * @param sal
     */
    public void addSearchBtnListener(ActionListener sal) {
        searchBtn.addActionListener(sal);
    }
	
    /***
     * openFileChooserDialog - displays file chooser dialog box 
     * @return
     */
    public int openFileChooserDialog(){
    	return gameFileFc.showOpenDialog(null);
      }
    
    /***
     * getFileName - filename getter
     * @return a string representing the filename choosen by the user 
     */
    public File getFileName(){
    	return gameFileFc.getSelectedFile();
      }

    /***
     * setResults - sets the results text area
     * @param result string to set the text area to
     */
    public void setResults(String result) {
        resultsTa.setText(result);
    }    
    
    /***
     * showError - displays error message to the user
     * @param errMessage a string representing the error message
     */
	public void showError(String errMessage) {
        JOptionPane.showMessageDialog(this, errMessage);	
	}
}
